/**************************************************************************
 *
 * Copyright 2014 MaeckySoft Ltd., Bludenz, Austria.
 * All Rights Reserved.
 *
 **************************************************************************/


#include "MGlyph.h"
#include "MTools.h"

//TODO: use std::string instead of (char *)

MGlyph::MGlyph(int iDebug = 0) : M3DBase(iDebug)
{

	if (iDebug >= MDBG_VERBOSE) std::cout << "(" << __FILE__ << ") <DBG_VERBOSE> " << __PRETTY_FUNCTION__ << " called." << std::endl;

    m_sBuffer = NULL;
    m_sImageRGB = NULL;
    m_sImageRGBA = NULL;
    m_lImageRGBASize= 0;
    m_lImageRGBSize = 0;

    m_iWidth = 0;
    m_iHeight = 0;
    //ctor
}

void MGlyph::printMethodInfo(std::string method)
{
	if (m_iDebug == MDBG_ALL) std::cout << "(" << __FILE__ << ") <DBG_ALL> " << method << " called." << std::endl;
}


unsigned char* MGlyph::getBuffer()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_sBuffer;
}
int MGlyph::getHeight()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_iHeight;
}
int MGlyph::getWidth()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_iWidth;
}

void MGlyph::setBuffer(unsigned char *sBuffer)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_sBuffer = sBuffer;
}
void MGlyph::setWidth(int iWidth)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_iWidth = iWidth;
}
void MGlyph::setHeight(int iHeight)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_iHeight = iHeight;
}

void MGlyph::setWidthPt(long lWidthPt)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_lWidthPt = lWidthPt;
}
void MGlyph::setHeightPt(long lHeightPt)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_lHeightPt = lHeightPt;
}

long MGlyph::getWidthPt()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_lWidthPt;
}
long MGlyph::getHeightPt()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_lHeightPt;
}
void MGlyph::setBearingX(long lBearingX)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_lBearingX = lBearingX;
}

void MGlyph::setBearingY(long lBearingY)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_lBearingY = lBearingY;
}
long MGlyph::getBearingX()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_lBearingX;
}
long MGlyph::getBearingY()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_lBearingY;
}
void MGlyph::setPointSize(int iPointsize)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_iPointsize = iPointsize;
}
void MGlyph::setAdvance(long lAdvance)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_lAdvance = lAdvance;
}
long MGlyph::getPointSize()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_iPointsize;
}
long MGlyph::getAdvance()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_lAdvance;
}

void MGlyph::setCharacter(char cCharacter)
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    m_cCharacter=cCharacter;
}
char MGlyph::getCharacter()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_cCharacter;
}

int MGlyph::renderRGB()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    if (m_sBuffer == NULL) return 1;
    if (m_sImageRGB != NULL){
        free(m_sImageRGB);
    }
	int iWidth, iHeight = 0;
	iWidth = this->getWidth();
	iHeight = this->getHeight();
    int iPixelsize=3;


    m_lImageRGBSize = sizeof(unsigned char) * iWidth * iHeight * iPixelsize;


    m_sImageRGB = new unsigned char[m_lImageRGBSize+1];
    memset(m_sImageRGB, 0 , m_lImageRGBSize +1);

	int x, y;
	unsigned char cTmpChar=0;
	for (y=0 ; y<iHeight ; y++) {
		for (x=0 ; x<iWidth ; x++) {
            cTmpChar = m_sBuffer[y*iWidth + x];
			m_sImageRGB[y*iWidth*iPixelsize + iPixelsize*x]=255-cTmpChar;
			m_sImageRGB[y*iWidth*iPixelsize + iPixelsize*x+1]=255-cTmpChar;
			m_sImageRGB[y*iWidth*iPixelsize + iPixelsize*x+2]=255-cTmpChar;
		}
    }
    return 0;
}

int MGlyph::renderRGBA()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    if (m_sBuffer == NULL) return 1;
    if (m_sImageRGBA != NULL){
        free(m_sImageRGBA);
    }
	int iWidth, iHeight = 0;
    int iPixelsize =4;
	iWidth = this->getWidth();
	iHeight = this->getHeight();

    m_lImageRGBASize = sizeof(unsigned char) * iWidth * iHeight * iPixelsize;
    m_sImageRGBA = new unsigned char[m_lImageRGBASize +1];
    memset(m_sImageRGBA, 0, m_lImageRGBASize +1);

	int x, y;
	unsigned char cTmpChar=0;
	for (y=0 ; y<iHeight ; y++) {
		for (x=0 ; x<iWidth; x++) {
            cTmpChar = m_sBuffer[y*iWidth + x];
			m_sImageRGBA[y*iWidth*iPixelsize + iPixelsize*x]=255-cTmpChar;
			m_sImageRGBA[y*iWidth*iPixelsize + iPixelsize*x+1]=255-cTmpChar;
			m_sImageRGBA[y*iWidth*iPixelsize + iPixelsize*x+2]=255-cTmpChar;

			if (cTmpChar == 0)
			{
                m_sImageRGBA[y*iWidth*iPixelsize + iPixelsize*x+3]=0;
			}
			else{
                m_sImageRGBA[y*iWidth*iPixelsize + iPixelsize*x+3]=255;
            }
		}
    }
    return 0;
}

unsigned long MGlyph::getImageRGBSize()
{
    return m_lImageRGBSize;
}

unsigned long MGlyph::getImageRGBASize()
{
    return m_lImageRGBASize;
}

unsigned char *MGlyph::getImageRGB()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_sImageRGB;
}

unsigned char *MGlyph::getImageRGBA()
{
	if (m_iDebug >= MDBG_ALL) this->printMethodInfo(__PRETTY_FUNCTION__);

    return m_sImageRGBA;
}


MGlyph::~MGlyph()
{
	if (m_iDebug >= MDBG_VERBOSE) std::cout << "(" << __FILE__ << ") <DBG_VERBOSE> " << __PRETTY_FUNCTION__ << " called." << std::endl;

    if (m_sBuffer != NULL)
    {
        delete [] m_sBuffer;
    }
    if (m_sImageRGB != NULL)
    {
        delete [] m_sImageRGB;
    }
    if (m_sImageRGBA != NULL)
    {

        delete [] m_sImageRGBA;

    }
    //dtor
}
